<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;//JWT式登录，用于接口
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Traits\UserCombineHelper;
use App\Models\Traits\UserFavouriteGoodsCalculateHelper;//计算客户的喜欢产品
use App\Models\Traits\UserRegisterCityHelper;
class User extends Authenticatable implements JWTSubject
{
    use Notifiable,SoftDeletes,UserCombineHelper,UserFavouriteGoodsCalculateHelper,UserRegisterCityHelper;

    /********************基本属性设置**************************/
    const COME_FROM_MINI    = 1;//客户来源，小程序
    const COME_FROM_ERCODE  = 2;//客户来源，二维码
    const COME_FROM_ADMIN   = 3;//客户来源，后台注册
    const COME_FROM_WEB     = 4;//客户来源，公众号
    const COME_FROM_PC      = 5;//客户来源，电脑端

    const SEX_MAN   =   1;//性别：男
    const SEX_WOMAN =   2;//性别：女
    const SEX_UNKNOWN=   3;//性别未知

    public static $comeFromMap=[
        self::COME_FROM_MINI => "小程序",
        self::COME_FROM_ERCODE=>"二维码",
        self::COME_FROM_ADMIN=>"后台注册",
        self::COME_FROM_WEB=>"公众号",
        self::COME_FROM_PC=>"电脑端",
    ];

    public static $sexMap = [
        self::SEX_MAN => "男",
        self::SEX_WOMAN=>"女",
        self::SEX_UNKNOWN=>"未知",
    ];
    public static $hidden_arr = [
        "created_at","updated_at",
        "l_m","come_from","cate_type","no_short_msg","birthday_year","birthday_md",
        "address","address_detail","has_wx","car_type","hobby","location_id","mark",
        "location_address","sex","deal_time",
        "phone_s","phone_t"
    ];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nickname',  'password',"phone","phone_s","phone_t",
        "log_time","l_m","come_from","cate_type","no_short_msg",
        "birthday_year","birthday_md","address","address_detail",
        "has_wx","car_type","hobby","location_id","mark","location_address",
        "sex","deal_time","web_openid","mini_openid","wx_mini_session_key",
        "wx_web_session_key","avatar","unionid","age_range","register_time","new_login_time","ip_addr"
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',"unionid",
        "web_openid","mini_openid","wx_mini_session_key"
        ,"wx_web_session_key"
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    /********************基本属性设置完毕**************************/


/*****************************表的关联*********************/
    //礼品券表
    public function coupons(){
        return $this->hasMany(Coupon::class);
    }
    //用户类别
    public function userType(){
        return $this->belongsTo(UserType::class,"cate_type");
    }
    //所在来源
    public function location(){
        return $this->belongsTo(Location::class);
    }
    //签到表的关联
    public function sinInLogs(){
        return $this->hasMany(SinInLog::class);
    }

    //用户收藏了啥
    public function collectionGoods(){
        return $this->hasMany(GoodsCollection::class);
    }
    //咨询表
    public function userAskForGoods(){
        return $this->hasMany(UserAskForGoods::class);
    }
    //积分表
    public function userIntegral(){
        return $this->hasOne(UserIntegral::class);
    }
    //积分记录变更表
    public function userIntegralLogs(){
        return $this->hasMany(UserIntegralLog::class);
    }

//    //用户访问表
//    public function userVisitLogs(){
//        return $this->hasMany(UserVisitLog::class);
//    }

    /*****************************表的关联完毕*********************/


    /************************jwt的接口实现*****************/
    /**
     * @inheritDoc
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
        // TODO: Implement getJWTIdentifier() method.
    }

    /**
     * @inheritDoc
     */
    public function getJWTCustomClaims()
    {
        return [];
        // TODO: Implement getJWTCustomClaims() method.
    }
    /************************jwt的接口实现完毕*****************/
    //***************************属性调取 以人类可接受的方式展示**************///

    //***************************属性调取完毕**************///

    //消息标记为已读
    public function markAsRead(){
        $this->notification_count = 0;
        $this->save();
        $this->unreadNotifications->markAsRead();
    }
    //发券通知
    public function couponNotify($instance){
        $this->increment("notification_count");
        $this->notify($instance);
    }
}
